﻿using Common;
using Vision001.Base;
using Vision001.Helper;
using Vision001.Models;
using Vision001.Views;
using MySqlConnector;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Vision001.ViewModels
{
    public class CapacityStatisticsViewModel : NotifyPorperChanged
    {
        private CapacityStatisticsView _capacityStatisticsView;
        public static updateHandler updatemessage;

        public CapacityStatisticsViewModel(CapacityStatisticsView capacityStatisticsView)
        {
            _capacityStatisticsView = capacityStatisticsView;
            CheckdaCommand = new Command() { ExeAction = new Action<object>(Checkcmd) };
            WritedaCommand = new Command() { ExeAction = new Action<object>(writedatacmd) };
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Checkcmd(object obj)
        {
            try
            {
                AskDatas = new ObservableCollection<SearchData>();
                //根据选中时间找到对应表名
                var TableName = _selectedDate.ToString("yyyy-MM-dd") + "capacity";
                AskDatas = new ObservableCollection<SearchData>(SqlSugarHelper.SqlRead(TableName, ChoosedateIndex));
            }
            catch (Exception e)
            {
                UpdateMessage(e.Message, LogLevel.Error);
            }
        }

        /// <summary>
        /// 模拟写入数据
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void writedatacmd(object obj)
        {
            try
            {
                SqlSugarHelper.SqlWrite(9999, true);
            }
            catch (Exception e)
            {
                UpdateMessage(e.Message, LogLevel.Error);
            }
        }

        /// <summary>
        /// 输出log
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="logLevel"></param>
        public void UpdateMessage(string msg, LogLevel logLevel = LogLevel.Info)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                updatemessage?.Invoke(msg, logLevel);
            });
        }

        #region 初始化属性和命令

        private ObservableCollection<SearchData> _askDatas;

        public ObservableCollection<SearchData> AskDatas
        {
            set
            {
                _askDatas = value;
                this.SetProperty();
            }
            get
            {
                return _askDatas;
            }
        }

        /// <summary>
        /// 选择的时间
        /// </summary>
        private DateTime _selectedDate = DateTime.Now;

        public DateTime SelectedDate
        {
            set
            {
                _selectedDate = value;
                this.SetProperty();
            }
            get => _selectedDate;
        }

        private int _choosedateIndex = 0;

        /// <summary>
        /// 选择白班还是夜班等
        /// </summary>
        public int ChoosedateIndex
        {
            set
            {
                _choosedateIndex = value;
                this.SetProperty();
            }
            get => _choosedateIndex;
        }

        private Command _checkdaCommand;

        public Command CheckdaCommand
        {
            get
            {
                return _checkdaCommand;
            }
            set
            {
                _checkdaCommand = value;
            }
        }

        private Command _writedaCommand;

        public Command WritedaCommand
        {
            get
            {
                return _writedaCommand;
            }
            set
            {
                _writedaCommand = value;
            }
        }

        #endregion 初始化属性和命令
    }
}